package trb.complexshield.bool;

import no.hrp.hvrc.rad.calc.intersect.Segment;
import no.hrp.hvrc.rad.dosim.bvh.BottumUpBVH;

public class IntersectableSegmentsProducer implements SegmentsProducer {

    public static boolean USE_AABB = true;
    public static int hitCnt = 0;
    public static int missCnt = 0;

    private final SegmentIntersectable intersectable;
    private final BottumUpBVH.AABB aabb;

    public IntersectableSegmentsProducer(SegmentIntersectable intersectable) {
        this.intersectable = intersectable;
        aabb = new BottumUpBVH.AABB(intersectable.getAABB());
    }

    @Override
    public Segments getSegments(Segment line, Segments out, SegmentsMemory memory) {
        out.clear();
        if (!USE_AABB || aabb.intersects(line)) {
            hitCnt++;
            float[] tMinMax = new float[2];
            if (intersectable.intersects(line, tMinMax)) {
                out.addAtEnd(tMinMax[0], tMinMax[1]);
            }
        } else {
            missCnt++;
        }
        return out;
    }
}
